{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-18T04:58:36.922795Z",
     "start_time": "2024-12-18T04:58:36.710729Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import os\n",
    "import imageio\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 指定目录\n",
    "directory = r'D:\\博士\\学位论文\\实验\\接触间断PINNs\\1D\\case1\\figure'  # 替换为你的图片文件夹路径\n",
    "\n",
    "# 获取按文件名排序的图片文件列表\n",
    "#file_names = sorted([f for f in os.listdir(directory) if f.endswith(('.jpg', '.png'))])\n",
    "file_names = sorted(\n",
    "    [f for f in os.listdir(directory) if f.endswith(('.jpg', '.png'))],\n",
    "    key=lambda x: int(''.join(filter(str.isdigit, x)))  # 提取数字进行排序\n",
    ")\n",
    "print(file_names)\n",
    "'''# 按顺序读取并显示每张图片\n",
    "for file_name in file_names:\n",
    "    image_path = os.path.join(directory, file_name)  # 组合成完整路径\n",
    "    image = imageio.imread(image_path)  # 读取图片\n",
    "    \n",
    "    # 显示图片\n",
    "    plt.imshow(image)\n",
    "    plt.axis('off')  # 不显示坐标轴\n",
    "    plt.title(file_name)  # 显示文件名\n",
    "    plt.show()  # 展示当前图片'''\n",
    "# 读取图片并存储在列表中\n",
    "images = []\n",
    "for file_name in file_names:\n",
    "    image_path = os.path.join(directory, file_name)  # 组合成完整路径\n",
    "    image = imageio.imread(image_path)  # 读取图片\n",
    "    images.append(image)  # 添加到列表中\n",
    "\n",
    "# 创建动画 GIF\n",
    "gif_path = '动画.gif'  # 替换为你想要保存的 GIF 文件名\n",
    "imageio.mimsave(gif_path, images, duration=500, loop=0)  # duration 控制每帧显示时间（秒）\n",
    "\n",
    "print(f\"动画已保存为 {gif_path}\")"
   ],
   "id": "a7919e4d6e56522a",
   "execution_count": 13,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-18T03:34:14.109095Z",
     "start_time": "2024-12-18T03:34:09.042326Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import torch\n",
    "from collections import OrderedDict\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import scipy.io\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\", category=UserWarning)  # 只忽略UserWarning类型的警告\n",
    "warnings.filterwarnings(\"ignore\", category=RuntimeWarning)  # 只忽略UserWarning类型的警告\n",
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "\n",
    "data = scipy.io.loadmat(r'1Dexact_Case1.mat')  # Import Solution data\n",
    "x_data = data['x'].flatten()[:, None]  # Partitioned spatial coordinates\n",
    "t_data = data['t'].flatten()[:, None]  # Partitioned spatial coordinates\n",
    "exact_rho = np.real(data['rho'])  # rho(t,x,y)\n",
    "data = scipy.io.loadmat(r'1Dpredict_Case1.mat')  # Import Solution data\n",
    "rho_predict = np.real(data['rho'])\n",
    "data = scipy.io.loadmat(r'PINN_1Dpredict_Case1.mat')  # Import Solution data\n",
    "PINNs_rho_predict = np.real(data['rho'])\n",
    "\n",
    "t_grid_data, x_grid_data = np.meshgrid(t_data, x_data)\n",
    "x_int_test = np.hstack((t_grid_data.flatten()[:, None], x_grid_data.flatten()[:, None]))\n",
    "\n",
    "# Define the levels for the contour plots based on the exact_rho range\n",
    "levels = np.linspace(exact_rho.min(), exact_rho.max(), 1000)\n",
    "# Custom ticks for the colorbar\n",
    "ticks = [0.1, 2, 4, 6, 8, 10]\n",
    "\n",
    "# 创建图形和子图\n",
    "fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(15, 5) ,sharey=True)\n",
    "\n",
    "# 绘制第一个等高线图\n",
    "c1 = ax1.contourf(x_grid_data, t_grid_data, exact_rho.T, levels=np.linspace(exact_rho.min(), exact_rho.max(), 1000), cmap='jet')\n",
    "ax1.set_title('exact_rho')\n",
    "ax1.set_xlabel('x')\n",
    "ax1.set_ylabel('t')\n",
    "'''# Add colorbar for the first subplot\n",
    "divider1 = make_axes_locatable(ax1)\n",
    "cax1 = divider1.append_axes(\"right\", size=\"5%\", pad=0.05)\n",
    "cbar1 = fig.colorbar(c1, cax=cax1, ticks=ticks)\n",
    "cbar1.set_ticklabels(ticks)'''\n",
    "\n",
    "# 绘制第二个等高线图\n",
    "c2 = ax2.contourf(x_grid_data, t_grid_data, rho_predict.T, levels=np.linspace(rho_predict.min(), rho_predict.max(), 1000), cmap='jet')\n",
    "ax2.set_title('decomposition_rho_predict')\n",
    "ax2.set_xlabel('x')\n",
    "'''# Add colorbar for the second subplot\n",
    "divider2 = make_axes_locatable(ax2)\n",
    "cax2 = divider2.append_axes(\"right\", size=\"5%\", pad=0.05)\n",
    "cbar2 = fig.colorbar(c2, cax=cax2, ticks=ticks)\n",
    "cbar2.set_ticklabels(ticks)'''\n",
    "\n",
    "# 绘制第三个等高线图\n",
    "c3 = ax3.contourf(x_grid_data, t_grid_data, PINNs_rho_predict.T, levels=np.linspace(PINNs_rho_predict.min(), PINNs_rho_predict.max(), 1000), cmap='jet')\n",
    "ax3.set_title('PINNs_rho_predict')\n",
    "ax3.set_xlabel('x')\n",
    "# Add colorbar for the third subplot\n",
    "divider3 = make_axes_locatable(ax3)\n",
    "cax3 = divider3.append_axes(\"right\", size=\"5%\", pad=0.05)\n",
    "cbar3 = fig.colorbar(c3, cax=cax3, ticks=ticks)\n",
    "cbar3.set_ticklabels(ticks)\n",
    "\n",
    "# Adjust layout to prevent overlap and save the figure\n",
    "plt.tight_layout()\n",
    "plt.savefig('rho_predict_compare.png')\n",
    "plt.show()"
   ],
   "id": "7b463b4aecf914e8",
   "execution_count": 4,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-10T06:55:53.336633Z",
     "start_time": "2024-12-10T06:55:52.831422Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n",
    "\n",
    "error_rho = abs(rho_predict - exact_rho).T\n",
    "PINNs_error_rho = abs(PINNs_rho_predict - exact_rho).T\n",
    "\n",
    "# Define the levels for the contour plots based on the exact_rho range\n",
    "levels = np.linspace(error_rho.min(), PINNs_error_rho.max(), 100)\n",
    "# Custom ticks for the colorbar\n",
    "ticks = list(np.around(np.linspace(error_rho.min(), PINNs_error_rho.max(), 10), decimals=3))\n",
    "# 创建图形和子图\n",
    "fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 3), sharey=True)\n",
    "\n",
    "# 绘制第一个等高线图\n",
    "c1 = ax1.contourf(x_grid_data, t_grid_data, error_rho, levels=levels, cmap='jet')\n",
    "ax1.set_title('decomposition_error_rho')\n",
    "ax1.set_xlabel('x')\n",
    "ax1.set_ylabel('t')\n",
    "\n",
    "# 绘制第二个等高线图\n",
    "c2 = ax2.contourf(x_grid_data, t_grid_data, PINNs_error_rho, levels=levels, cmap='jet')\n",
    "ax2.set_title('PINNs_error_rho')\n",
    "ax2.set_xlabel('x')\n",
    "# Add colorbar for the second subplot\n",
    "divider2 = make_axes_locatable(ax2)\n",
    "cax2 = divider2.append_axes(\"right\", size=\"5%\", pad=0.05)\n",
    "cbar2 = fig.colorbar(c2, cax=cax2, ticks=ticks)\n",
    "cbar2.set_ticklabels(ticks)\n",
    "\n",
    "# 显示图形\n",
    "plt.tight_layout()  # 调整布局以防止重叠\n",
    "plt.savefig('error_rho_compare.png')\n",
    "plt.show()\n",
    "\n",
    "'''# 第一个图像\n",
    "plt.figure(figsize=(5, 2))\n",
    "contour = plt.contourf(x_grid_data, t_grid_data, error_rho, levels=np.linspace(error_rho.min(), PINNs_error_rho.max(), 1000), cmap='jet')\n",
    "plt.colorbar(contour)\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('t')\n",
    "plt.title('error_rho')\n",
    "plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1)\n",
    "plt.savefig('error_rho.png', dpi=300)  # 保存图像并指定DPI\n",
    "plt.show()\n",
    "\n",
    "\n",
    "# 第二个图像\n",
    "plt.figure(figsize=(5, 2))\n",
    "contour = plt.contourf(x_grid_data, t_grid_data, PINNs_error_rho, levels=np.linspace(error_rho.min(), PINNs_error_rho.max(), 1000), cmap='jet')\n",
    "plt.colorbar(contour)\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('t')\n",
    "plt.title('PINNs_error_rho')\n",
    "plt.savefig('PINNs_error_rho.png')\n",
    "plt.show()\n",
    "'''"
   ],
   "id": "d755c4410a3922fe",
   "execution_count": 106,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-10T07:05:05.062525Z",
     "start_time": "2024-12-10T07:05:04.641271Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure()\n",
    "plt.plot(x_data, exact_rho[0, :], linewidth=2, label='Exact_rho')\n",
    "plt.scatter(x_data, rho_predict[0, :], label='rho_predict')\n",
    "plt.scatter(x_data, PINNs_rho_predict[0, :], label='PINNs_rho_predict')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('rho')\n",
    "plt.legend()\n",
    "plt.title('rho_init')\n",
    "plt.savefig('rho_init_compare.png')\n",
    "plt.show()\n",
    "\n",
    "plt.figure()\n",
    "plt.plot(x_data[10:100], exact_rho[0, 10:100], linewidth=2, label='Exact_rho')\n",
    "plt.scatter(x_data[10:100], rho_predict[0, 10:100], label='rho_predict')\n",
    "plt.scatter(x_data[10:100], PINNs_rho_predict[0, 10:100], label='PINNs_rho_predict')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('rho')\n",
    "plt.legend()\n",
    "plt.title('rho_init')\n",
    "plt.savefig('rho_init_open_compare.png')\n",
    "plt.show()"
   ],
   "id": "bd7568230dcc081a",
   "execution_count": 107,
   "outputs": []
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-18T05:51:27.932114Z",
     "start_time": "2024-12-18T05:51:27.248480Z"
    }
   },
   "cell_type": "code",
   "source": [
    "plt.figure(figsize=(8, 5))\n",
    "plt.plot(x_data, exact_rho[-1, :], linewidth=2, label='Exact_rho')\n",
    "plt.scatter(x_data, rho_predict[-1, :], label='rho_predict')\n",
    "plt.scatter(x_data, PINNs_rho_predict[-1, :], label='PINNs_rho_predict')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('rho')\n",
    "plt.legend()\n",
    "plt.title('rho_end')\n",
    "plt.savefig('rho_end_compare.png')\n",
    "plt.show()\n",
    "\n",
    "plt.figure(figsize=(5, 3))\n",
    "plt.plot(x_data[140:156], exact_rho[-1, 140:156], linewidth=2, label='Exact_rho')\n",
    "plt.scatter(x_data[140:156], rho_predict[-1, 140:156], label='rho_predict')\n",
    "plt.scatter(x_data[140:156], PINNs_rho_predict[-1, 140:156], label='PINNs_rho_predict')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('rho')\n",
    "plt.legend()\n",
    "#plt.title('rho_end')\n",
    "plt.savefig('间断rho_end_compare.png')\n",
    "plt.show()\n",
    "\n",
    "plt.figure(figsize=(8, 5))\n",
    "plt.plot(x_data[10:100], exact_rho[-1, 10:100], linewidth=2, label='Exact_rho')\n",
    "plt.scatter(x_data[10:100], rho_predict[-1, 10:100], label='rho_predict')\n",
    "plt.scatter(x_data[10:100], PINNs_rho_predict[-1, 10:100], label='PINNs_rho_predict')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('rho')\n",
    "#plt.legend()\n",
    "#plt.title('rho_end')\n",
    "plt.savefig('smooth_rho_end_open_compare.png')\n",
    "plt.show()\n",
    "\n",
    "plt.figure(figsize=(8, 5))\n",
    "plt.plot(x_data[10:150], exact_rho[-1, 10:150], linewidth=2, label='Exact_rho')\n",
    "plt.scatter(x_data[10:150], rho_predict[-1, 10:150], label='rho_predict')\n",
    "plt.scatter(x_data[10:150], PINNs_rho_predict[-1, 10:150], label='PINNs_rho_predict')\n",
    "plt.xlabel('x')\n",
    "plt.ylabel('rho')\n",
    "#plt.legend()\n",
    "#plt.title('rho_end')\n",
    "plt.savefig('rho_end_open_compare.png')\n",
    "plt.show()"
   ],
   "id": "6f7e6b04322365d",
   "execution_count": 14,
   "outputs": []
  },
  {
   "metadata": {},
   "cell_type": "code",
   "execution_count": null,
   "source": "",
   "id": "7d0bf81c01f33bc8",
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
